var app = getApp();
var QQMapWX = require('../../qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  onShow() {
    if (app.buys_car.title == '' && app.buys_car.color == '' && app.buys_car.version==''){
        show: false,
        console.log('没有物品')
    }else{
      for(var k in this.data.choose_arr){
        var arr_id = 'choose_arr[' + k + '].id';
        var arr_title = 'choose_arr[' + k + '].title';
        var arr_color = 'choose_arr[' + k + '].color';
        var arr_version = 'choose_arr[' + k + '].version';
        var arr_com = 'choose_arr[' + k + '].com';
        var arr_img = 'choose_arr[' + k + '].img';
        var arr_choose_num = 'choose_arr[' + k + '].choose_num';
        var arr_money = 'choose_arr[' + k + '].money';
        var arr_store_title = 'choose_arr[' + k + '].store_title';
        var arr_total_number = 'choose_arr[' + k + '].total_number';
        this.setData({
          [arr_id]: app.buys_car.id,
          [arr_title]: app.buys_car.title,
          [arr_color]: app.buys_car.color,
          [arr_version]: app.buys_car.version,
          [arr_com]: app.buys_car.com,
          [arr_img]: '../' + app.buys_car.img,
          [arr_choose_num]: app.buys_car.choose_num,
          [arr_money]: app.buys_car.money,
          [arr_total_number]: app.buys_car.total_number,
          store_logo: '../../img/店铺.png',
          [arr_store_title]: ['官方自营店'],
          show: true,
        })
      }
      
      this.calculation('up')
    }
    qqmapsdk = new QQMapWX({
      key:'TBMBZ-3G7CW-FECRA-REKKI-Q7MNT-P7F6W'
      })
      this.address()
  },
  data: {
    show:false,
    choose_arr:[//使用data的原数据暂存，然后存入此数组作渲染，这样会让多个商品渲染
      {
        'id':'',
        'store_title': '',
        'title': '',
        'color': '',
        'version': '',
        'com': '',
        'img': '',
        'total_number':'',
        'choose_num': '',
        'money': '',
      }
    ],
    money_real: '0',
    store_logo: '',
    choose:false,
    this_address: '无法获取当前位置',
  },
  up: function (e) {
    var arr_choose_num = 'choose_arr[' + 0 + '].choose_num';
    this.setData({
      [arr_choose_num]: this.data.choose_arr[0].choose_num + 1,
      choose:true
    })
    this.calculation('up')
  },
  down: function (e) {
    if (this.data.choose_arr[0].choose_num > 1) {
      var arr_choose_num = 'choose_arr[' + 0 + '].choose_num';
      this.setData({
        [arr_choose_num]: this.data.choose_arr[0].choose_num - 1,
        choose: true
      })
    }
    this.calculation('down')
  },
  calculation: function (str) {
    if (str == 'up') {
      var money = this.data.choose_arr[0].money;
      var num = this.data.choose_arr[0].choose_num;
      var total_number = money * num;
      this.setData({
        money_real: total_number
      })
    } else {
      var money = this.data.choose_arr[0].money;
      var num = this.data.choose_arr[0].choose_num;
      var total_number = money / num;
      this.setData({
        money_real: total_number
      })
    }
  },
  choose_this:function(e){
    var id=e.currentTarget.id
    if(this.data.choose==false){
      var arr_money = this.data.choose_arr[id].money;
      this.setData({
        choose: true,
        money_real: arr_money
      })
      
    }else{
      this.setData({
        choose: false,
        money_real: '0'
      })
    }
      
  },
  address: function () {
    var then = this
    wx.authorize({
      scope: 'scope.userLocation',
      success() {
        wx.getLocation({
          success: function (res) {
            var latitude_this = res.latitude;
            var longitude_this = res.longitude;
            console.log(latitude_this)
            console.log(longitude_this)
            qqmapsdk.reverseGeocoder({
              location: {
                latitude: latitude_this,
                longitude: longitude_this
              },
              success: function (res) {
                then.setData({
                  this_address: res.result.address
                })
              },
              fail: function (res) {
                console.log(res);
              },
            });
          }
        })
      }
    })
  },
  dell:function(){
    wx.showToast({
      title: '请长按商品删除',
      icon:'none'
    })
  },
  dell_all:function(e){
    var this_id=e.currentTarget.id;
    var then=this
    wx.showModal({
      title: '确定删除吗',
      content: '删除本条商品',
      success: function (res) {
        if (res.confirm) {
         // var choose_arr=then.data.choose_arr;
          console.log(then.data.choose_arr)
          for (var k in then.data.choose_arr){
            if(then.data.choose_arr[k].id==this_id){
              var arr_id = 'choose_arr[' + k + '].id';
              var arr_title = 'choose_arr[' + k + '].title';
              var arr_color = 'choose_arr[' + k + '].color';
              var arr_version = 'choose_arr[' + k + '].version';
              var arr_com = 'choose_arr[' + k + '].com';
              var arr_img = 'choose_arr[' + k + '].img';
              var arr_choose_num = 'choose_arr[' + k + '].choose_num';
              var arr_money = 'choose_arr[' + k + '].money';
              var arr_store_title = 'choose_arr[' + k + '].store_title';
              var arr_total_number = 'choose_arr[' + k + '].total_number';
              then.setData({
                [arr_id]: '',
                [arr_title]: '',
                [arr_color]: '',
                [arr_version]: '',
                [arr_com]: '',
                [arr_img]: '',
                [arr_choose_num]: '',
                [arr_money]: '',
                [arr_total_number]: '',
                store_logo: '',
                [arr_store_title]: '',
                show: false,
                money_real: '0'
              })
            }
          }
          app.buys_car.id='';
          app.buys_car.title=''
          app.buys_car.color = ''
          app.buys_car.version = ''
          app.buys_car.com = ''
          app.buys_car.img = ''
          app.buys_car.choose_num = ''
          app.buys_car.money = ''
          app.buys_car.total_number = ''
          console.log(app.buys_car)
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  buys_determine:function(){
    var then={};
    then.title = this.data.choose_arr[0].title;
    then.color = this.data.choose_arr[0].color;
    then.com = this.data.choose_arr[0].com;
    then.version = this.data.choose_arr[0].version;
    then.img = this.data.choose_arr[0].img;
    then.num = this.data.choose_arr[0].choose_num;
    then.money = this.data.choose_arr[0].money;
    var model = JSON.stringify(then);
    wx.navigateTo({
      url: '../../buys_determine/buys_determine?model='+model,
    })
  }
})